package com.taiyi.renaissancemall.ui.mine.address.add;

import android.content.Intent;
import android.util.Log;

import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.Utils.Utils;
import com.taiyi.renaissancemall.base.BasePresent;
import com.taiyi.renaissancemall.base.MyApp;
import com.taiyi.renaissancemall.bean.AddressInfoBean;
import com.taiyi.renaissancemall.bean.BaseResultBean;
import com.taiyi.renaissancemall.bean.EmptyBean;
import com.taiyi.renaissancemall.bean.ProvinceBean;
import com.taiyi.renaissancemall.net.BaseUrl;
import com.taiyi.renaissancemall.net.HttpUtils;
import com.taiyi.renaissancemall.net.callbck.JsonCallback;
import com.taiyi.renaissancemall.ui.login.LoginActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Response;

public class AddAddressPresent extends BasePresent<AddAddressView> {

    public void getAddressLevelList(String id, int type) {
        Map<String, String> map = new HashMap<>();
        map.put("area_id", id);
        HttpUtils.postRequest(BaseUrl.GET_PROVINCE_CITY_COUNTRY, this, map, new JsonCallback<BaseResultBean<List<ProvinceBean>>>() {
            @Override
            public BaseResultBean<List<ProvinceBean>> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<List<ProvinceBean>>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.getProvinceListSuccess(response.body().data, type);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.getProvinceListFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<List<ProvinceBean>>> response) {
                super.onError(response);
            }
        });
    }


    public void addAddress() {
        Map<String, String> map = new HashMap<>();
        map.put("name", view.getReceiverName());
        map.put("tel", view.getPhoneNumber());
        map.put("provinceid", view.getSelProvinceId());
        map.put("cityid", view.getSelCityId());
        map.put("areaid", view.getSelDistrictId());
        map.put("address", view.getDetailAddress());
        map.put("iftype", view.ifDefault());
        Log.i("ifType", "---" + view.ifDefault());
        HttpUtils.postRequest(BaseUrl.ADD_ADDRESS, this, map, new JsonCallback<BaseResultBean<EmptyBean>>() {
            @Override
            public BaseResultBean<EmptyBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<EmptyBean>> response) {
                super.onSuccess(response);
                ToastUtils.showLongToast(response.body().msg_cn);
                if (response.body().code == 0) {
                    view.addAddressSuccess();
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.addAddressFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<EmptyBean>> response) {
                super.onError(response);
            }
        });
    }


    public void getAddressInfo() {
        Map<String, String> map = new HashMap<>();
        map.put("useraddress_id", view.getAddressId());
        HttpUtils.postRequest(BaseUrl.GET_ADDRESS_INFO, this, map, new JsonCallback<BaseResultBean<AddressInfoBean>>() {
            @Override
            public BaseResultBean<AddressInfoBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<AddressInfoBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.getAddressInfoSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.getAddressInfoFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<AddressInfoBean>> response) {
                super.onError(response);
            }
        });
    }


    public void updateAddress() {
        Map<String, String> map = new HashMap<>();
        map.put("useraddress_id",view.getAddressId());
        map.put("name", view.getReceiverName());
        map.put("tel", view.getPhoneNumber());
        map.put("provinceid", view.getSelProvinceId());
        map.put("cityid", view.getSelCityId());
        map.put("areaid", view.getSelDistrictId());
        map.put("address", view.getDetailAddress());
        map.put("iftype", view.ifDefault());
        Log.i("ifType", "---" + view.ifDefault());
        HttpUtils.postRequest(BaseUrl.UPDATE_ADDRESS, this, map, new JsonCallback<BaseResultBean<EmptyBean>>() {
            @Override
            public BaseResultBean<EmptyBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<EmptyBean>> response) {
                super.onSuccess(response);
                ToastUtils.showLongToast(response.body().msg_cn);
                if (response.body().code == 0) {
                    view.addAddressSuccess();
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.addAddressFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<EmptyBean>> response) {
                super.onError(response);
            }
        });
    }


}
